{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**1. Two Sum** <font color=green size=3>_easy_</font>\n",
    "\n",
    "Given an array of integers, return indices of the two numbers such that they add up to a specific target.\n",
    "You may assume that each input would have exactly one solution, and you may not use the same element twice.\n",
    "\n",
    "**Example**\n",
    "```\n",
    "Given nums = [2, 7, 11, 15], target = 9,  \n",
    "Because nums[0] + nums[1] = 2 + 7 = 9,  \n",
    "return [0, 1].  \n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 5]\n"
     ]
    }
   ],
   "source": [
    "def two_sum(nums, target):\n",
    "    \"\"\"\n",
    "    Given nums = [2, 7, 11, 15], target = 9,\n",
    "    Because nums[0] + nums[1] = 2 + 7 = 9,\n",
    "    return [0, 1].\n",
    "    \"\"\"\n",
    "    reverted = {}\n",
    "    for i in range(len(nums)):\n",
    "        if reverted.get(nums[i]) is not None:\n",
    "            return [reverted.get(nums[i]), i]\n",
    "        else:\n",
    "            reverted[target - nums[i]] = i\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    nums = [2,7,9,15,3,4,6]\n",
    "    target = 13\n",
    "    ret = two_sum(nums, target)\n",
    "    print(ret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
